<?php
class Con_kantor extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('auth');
		$this->load->model('kantormodel');
		$this->load->model('usermodel');
	}
	public function list_kantor()
	{
		$this->auth->restrict();
		$data['menu'] = $this->usermodel->get_menu();
		$data['all_kantor'] = $this->kantormodel->get_all_kantor();
		$this->template->set('title','Daftar Kantor | Aplikasi Persuratan');
		$this->template->load('template','list_kantor',$data);
	}
	public function tambah()
	{
		$this->auth->restrict();
		$this->load->library('form_validation');
		$this->form_validation->set_rules('kd_kantor','kd_kantor','trim|required');
		$this->form_validation->set_rules('nama_kantor','nama_kantor','trim|required');
		$this->form_validation->set_rules('alamat','alamat','trim|required');
		$this->form_validation->set_rules('kota','kota','trim|required');
		$this->form_validation->set_rules('kodepos','kodepos','trim|required');
		$this->form_validation->set_rules('telp','telp','trim|required');
		$this->form_validation->set_rules('fax','fax','trim|required');
		$this->form_validation->set_rules('website','website','trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = $this->usermodel->get_menu();
			$this->template->set('title','Tambah Penanda Tangan | Aplikasi Persuratan');
			$this->template->load('template','form_tambah_kantor',$data);
		}
		else
		{
			$data_kantor = array (
			'kd_kantor' =>$this->input->post('kd_kantor'),
			'nama_kantor' =>$this->input->post('nama_kantor'),
			'alamat' =>$this->input->post('alamat'),
			'kota' =>$this->input->post('kota'),
			'kodepos' =>$this->input->post('kodepos'),
			'telp' =>$this->input->post('telp'),
			'fax' =>$this->input->post('fax'),
			'website' =>$this->input->post('website')
			);
			$this->kantormodel->tambah_data_kantor($data_kantor);
			//kembali ke halaman list kantor
			redirect ('con_kantor/list_kantor');
		}
	}
	public function edit()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('kd_kantor','kd_kantor','trim|required');
		$this->form_validation->set_rules('nama_kantor','nama_kantor','trim|required');
		$this->form_validation->set_rules('alamat','alamat','trim|required');
		$this->form_validation->set_rules('kota','kota','trim|required');
		$this->form_validation->set_rules('kodepos','kodepos','trim|required');
		$this->form_validation->set_rules('telp','telp','trim|required');
		$this->form_validation->set_rules('fax','fax','trim|required');
		$this->form_validation->set_rules('website','website','trim|required');
	
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		$kd_kantor = $this->uri->segment(3);
		
		if ($this->form_validation->run() == FALSE)
		{
			//$data['level'] = $this->usermodel->get_all_level();
			//dapatkan user yang akan diedit
			$data['menu'] = $this->usermodel->get_menu();
			$data['kantor'] = $this->kantormodel->get_kantor_by_kode($kd_kantor);
			$this->template->set('title','Edit Data Kantor | Aplikasi Persuratan');
			$this->template->load('template','form_edit_kantor',$data);
		}
		else
		{
			$data_kantor = array (
			'kd_kantor' =>$this->input->post('kd_kantor'),
			'nama_kantor' =>$this->input->post('nama_kantor'),
			'alamat' =>$this->input->post('alamat'),
			'kota' =>$this->input->post('kota'),
			'kodepos' =>$this->input->post('kodepos'),
			'telp' =>$this->input->post('telp'),
			'fax' =>$this->input->post('fax'),
			'website' =>$this->input->post('website')
			);
			$this->kantormodel->edit_data_kantor($data_kantor,$kd_kantor);
			//kembali ke halaman list kantor
			redirect ('con_kantor/list_kantor');
		}
	}
	public function hapus()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		//dapatkan id user dari segment ke-3 dari URI
		$kode = $this->uri->segment(3);
		$this->kantormodel->delete_kantor($kode);
		//kembali ke halaman list kantor
		redirect ('con_kantor/list_kantor');
	}
		
}